package com.zbb.exam_system.controller;

import cn.afterturn.easypoi.exception.excel.ExcelImportException;
import com.zbb.exam_system.constant.Method;
import com.zbb.exam_system.dto.JsonResult;
import com.zbb.exam_system.dto.TableDto;
import com.zbb.exam_system.entity.Paper;
import com.zbb.exam_system.exception.ExamException;
import com.zbb.exam_system.service.PaperService;
import com.zbb.exam_system.utils.HttpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zbbstart
 * @date 2021/2/12 - 13:05
 */
@RestController
@RequestMapping("papers")
@Api(tags = "试卷相关功能的接口")
public class PaperController {
    @Autowired
    private PaperService paperService;
    @Autowired
    private HttpUtils httpUtils;

    /**
     * 获取当前老师创建的所有试卷
     */
    @PreAuthorize("hasRole('ROLE_TEACHER')")
    @GetMapping("{teacherNo}")
    @ApiOperation(value = "获取试卷", notes = "根据老师的编号获取试卷")
    public JsonResult getPaperByTeacherNo(@PathVariable("teacherNo") Integer teacherNo,
                                          HttpServletRequest req, HttpServletResponse res) {
        JsonResult result = new JsonResult();
        try {
            int pageNo = Integer.parseInt(req.getParameter("page"));
            int pageSize = Integer.parseInt(req.getParameter("limit"));
            TableDto tableDto = paperService.getPapersByTeacherNo(teacherNo, pageNo, pageSize);
            result = httpUtils.buildSuccess(Method.GET, tableDto);
        } catch (ExamException e) {
            result = httpUtils.buildFailure(e.getMessage(), Method.GET);
        } catch (Exception e) {
            e.printStackTrace();
            result = httpUtils.build500();
        }
        res.setStatus(result.getCode());
        return result;
    }


    /**
     * 创建试卷
     */
    @PreAuthorize("hasRole('ROLE_TEACHER')")
    @PostMapping("{teacherNo}")
    @ApiOperation(value = "创建试卷", notes = "根据老师的编号创建试卷")
    public JsonResult createPaper(@PathVariable("teacherNo") Integer teacherNo,
                                  @RequestBody Paper paper, HttpServletResponse res) {
        JsonResult result = new JsonResult();
        try {
            paper.setTeacherNo(teacherNo);
            paperService.createPaper(paper);
            result = httpUtils.buildSuccess(Method.POST, "创建试卷成功...");
        } catch (ExamException e) {
            result = httpUtils.buildFailure(e.getMessage(), Method.POST);
        } catch (Exception e) {
            e.printStackTrace();
            result = httpUtils.build500();
        }
        res.setStatus(result.getCode());
        return result;
    }

    /**
     * 生成试卷 generate
     */
    @PreAuthorize("hasRole('ROLE_TEACHER')")
    @GetMapping(value = "{paperId}",params = "_generate")
    @ApiOperation(value = "生成试卷", notes = "根据试卷的编号生成试卷")
    public JsonResult generatePaper(@PathVariable("paperId") Integer paperId,HttpServletResponse res){
        JsonResult result = new JsonResult();
        try {
            paperService.generatePaper(paperId);
            result = httpUtils.buildSuccess(Method.GET,"生成试卷成功...");
        }catch (ExamException e){
            result = httpUtils.buildFailure(e.getMessage(), Method.GET);
        }catch (Exception e) {
            e.printStackTrace();
            result = httpUtils.build500();
        }
        res.setStatus(result.getCode());
        return result;
    }
    /**
     * 获取试卷的详细信息
     */
    @PreAuthorize("hasRole('ROLE_TEACHER')")
    @GetMapping(value = "info/{paperId}")
    @ApiOperation(value = "获取试卷所有信息", notes = "根据试卷的编号获取试卷的所有信息")
    public JsonResult getPaperAllInfo(@PathVariable("paperId") Integer paperId,HttpServletResponse res) {
        JsonResult result = new JsonResult();
        try {
            Paper paper = paperService.getPaperAllInfo(paperId);
            result.setCode(200);
            result.setData(paper);
        }catch (ExamException | ExcelImportException e){
            result = httpUtils.buildFailure(e.getMessage(), Method.GET);
        } catch (Exception e) {
            e.printStackTrace();
            result = httpUtils.build500();
        }
        res.setStatus(result.getCode());
        return result;
    }
    /**
     * 使试卷过期
     */
    @PreAuthorize("hasRole('ROLE_TEACHER')")
    @GetMapping(value = "{paperId}",params = "_cancel")
    @ApiOperation(value = "使试卷过期", notes = "根据试卷的编号使试卷过期")
    public JsonResult cancelPaper(@PathVariable("paperId") Integer paperId,HttpServletResponse res){
        JsonResult result = new JsonResult();
        try {
            paperService.cancelPaper(paperId);
            result = httpUtils.buildSuccess(Method.GET,"作废试卷成功...");
        } catch (Exception e) {
            e.printStackTrace();
            result = httpUtils.build500();
        }
        res.setStatus(result.getCode());
        return result;
    }
    /**
     * 删除试卷
     */
    @PreAuthorize("hasRole('ROLE_TEACHER')")
    @DeleteMapping("{paperId}")
    @ApiOperation(value = "删除试卷", notes = "根据试卷的编号删除试卷的所有相关信息")
    public JsonResult deletePaper(@PathVariable("paperId") Integer paperId,HttpServletResponse res){
        JsonResult result = new JsonResult();
        try {
            paperService.deletePaper(paperId);
            result = httpUtils.buildSuccess(Method.DELETE);
            result.setData("删除试卷成功...");
        } catch (Exception e) {
            e.printStackTrace();
            result = httpUtils.build500();
        }
        res.setStatus(result.getCode());
        return result;
    }
}
